package com.example.testnews.activity;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MenuItem;
import android.view.Window;
import android.widget.Switch;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.testnews.R;
import com.example.testnews.adapter.AppFragmentAdapter;
import com.example.testnews.fragmant.CollectFragment;
import com.example.testnews.fragmant.HomeFragment;
import com.example.testnews.fragmant.SettingsFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private BottomNavigationView menu;
    private Switch aSwitch;
    private static final String TAG = "FragmentActivity";

    @Override
    public Resources getResources() {
        Resources resources = super.getResources();
        Configuration newConfig = resources.getConfiguration();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        if (resources != null && newConfig.fontScale != 1) {
            newConfig.fontScale = 1;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                Context configurationContext = createConfigurationContext(newConfig);
                resources = configurationContext.getResources();
                displayMetrics.scaledDensity = displayMetrics.density * newConfig.fontScale;
            } else {
                resources.updateConfiguration(newConfig, displayMetrics);
            }
        }
        return resources;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView(){
        viewPager = findViewById(R.id.viewPager);
        menu = findViewById(R.id.menu);

        HomeFragment homeFragment = new HomeFragment();
        CollectFragment collectFragment = new CollectFragment();
        SettingsFragment settingsFragment = new SettingsFragment();

        List<Fragment> fragments = new ArrayList<>();
        fragments.add(homeFragment);
        fragments.add(collectFragment);
        fragments.add(settingsFragment);

        AppFragmentAdapter appFragmentAdapter = new AppFragmentAdapter(getSupportFragmentManager(),0,fragments);

        viewPager.setAdapter(appFragmentAdapter);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
              //  Log.e(TAG, "position:+++"+position+"positionOffset:+++"+positionOffset+"positionOffsetPixels:+++"+positionOffsetPixels);

            }

            @Override
            public void onPageSelected(int position) {
                Log.e("====", position + "");

//                menu.getMenu().getItem(position).set
                switch (position){
                    case 0:
                        menu.setSelectedItemId(R.id.menu_home);
                        break;
                    case 1:
                        menu.setSelectedItemId(R.id.menu_collect);
                        break;
                    case 2:
                        menu.setSelectedItemId(R.id.menu_contacts);
                        break;
                    default:
                        break;

                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                String result= "";
                switch (state){
                    case ViewPager.SCROLL_STATE_IDLE:result = "SCROLL_STATE_IDLE";break;
                    case ViewPager.SCROLL_STATE_DRAGGING:result="SCROLL_STATE_DRAGGING";break;
                    case ViewPager.SCROLL_STATE_SETTLING:result="SCROLL_STATE_SETTLING";break;
                }

                Log.d(TAG, "onPageScrollStateChanged: "+result);
            }
        });

        viewPager.setCurrentItem(0);

        menu.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.menu_home:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.menu_collect:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.menu_contacts:
                        viewPager.setCurrentItem(2);
                        break;
                }
                return true;
            }
        });


    }
}